﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml.Linq;
using System.Reflection.Metadata;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Castle.Core.Resource;
using MailKit.Search;
using Mysqlx;
using Mysqlx.Crud;
using Meiam.System.Core;
using MySqlX.XDevAPI;
using System.Net.Http;
using System.Security.Policy;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    ///  组件WMS_成品货主更新
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductOwnerUpdateController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_oa;
        public readonly string gz_con;


        public ProductOwnerUpdateController(ILogger<ProductOwnerUpdateRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            con_oa = _configuration.GetConnectionString("OA_DbConnection");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            _logger = loggerFactory.CreateLogger<ProductOwnerUpdateController>();
        }

        /// <summary>
        /// 组件WMS_成品货主更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostProductOwnerUpdate([FromBody] ProductOwnerUpdateRequestDto strparm)
        {
            string strMsg = "";
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            ProductOwnerUpdateResponseDto re = new ProductOwnerUpdateResponseDto();
            try
            {
                dic = new Dictionary<string, string>(); //查询公用dic
                dic.Add("source_no", "");
                string strSql1 = MethodHelp.GetTranSQL("TMS0229", dic, con);//获取WMS送货数据SQL
                if (strSql1 != "")
                {
                    DataTable dt_cb = DB.OracleQuery(strSql1, gz_con);
                    if (dt_cb.Rows.Count>0)
                    {
                        foreach (DataRow item in dt_cb.Rows)
                        {
                            dic = new Dictionary<string, string>(); //查询公用dic
                            dic.Add("erp_mo_code", item["LOTATT05"].ToString());
                            DataTable hz_dt = MethodHelp.GetTableInfo("TMS0227", dic,con);
                            if (hz_dt.Rows.Count>0)
                            {
                                dic = new Dictionary<string, string>(); //查询公用dic
                                dic.Add("SOURCE_NO", hz_dt.Rows[0]["ccode"].ToString());
                                dic.Add("customer_name", hz_dt.Rows[0]["customer_name"].ToString());
                                dic.Add("IS_OWNER", "有主");
                                dic.Add("MO_NO", item["LOTATT05"].ToString());
                                string strSql2 = MethodHelp.GetTranSQL("TMS0230", dic, con);//获取WMS送货数据SQL
                                if (strSql2!=""|| strSql2!=null)
                                {
                                    strSqlList.Add(strSql2);
                                }
                            }
                            else
                            {
                                dic = new Dictionary<string, string>(); //查询公用dic
                                dic.Add("SOURCE_NO", "");
                                dic.Add("customer_name", "");
                                dic.Add("IS_OWNER", "无主");
                                dic.Add("MO_NO", item["LOTATT05"].ToString());
                                string strSql2 = MethodHelp.GetTranSQL("TMS0230", dic, con);//获取WMS送货数据SQL
                                if (strSql2 != "" || strSql2 != null)
                                {
                                    strSqlList.Add(strSql2);
                                }
                            }

                        }
                        if (strSqlList.Count > 0)
                        {
                            bool A = DB.OracleExecuteNoQueryTran(strSqlList, gz_con);
                            if (A)
                            {

                            }
                            else
                            {
                                strMsg = "Database update failed";
                            }

                        }
                    }
                }





            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;

            }


            //_logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }       
    }

}
